//@ts-nocheck
//请求
import { message } from 'antd';
import axios from 'axios';

const instance = axios.create({
  baseURL: '/api/v1',
  responseType: 'json',
  timeout: 5000,
});

instance.interceptors.request.use(
  (config: any) => {
    // 发送请求
    return config;
  },
  (err) => {
    return Promise.reject(err);
  },
);

instance.interceptors.response.use(
  (response: any) => {
    if (response.data.code !== 200) {
      message.error(response.data.message);
    }
    return response;
  },
  (error) => {
    const err = JSON.parse(JSON.stringify(error));
    if (err?.status === 401) {
      const errorMsg = '登录超时，请重新登录';
      infoModalFactory(errorMsg);
      flag = false;
    } else if (err?.code === 500) {
      message.error(err?.message);
    }
    return Promise.reject(error);
  },
);

export interface ResponseType<T> {
  code: number;
  message: string;
  data: T;
  paginate?: {
    page: number;
    page_size: number;
    total: number;
    total_pages: number;
  };
}

export const postClient = async <T>(
  url: string,
  params = {},
): Promise<ResponseType<T>> => {
  const { data, config } = await instance.post(url, params);
  data.xhrDetail = config;
  return data;
};
export const putClient = async <T>(
  url: string,
  params = {},
): Promise<ResponseType<T>> => {
  const { data } = await instance.put(url, params);
  return data;
};
export const deleteClient = async (url: string, params = {}) => {
  const { data } = await instance.delete(url, { params });
  return data;
};
export const getClient = async <T>(
  url: string,
  params = {},
): Promise<ResponseType<T>> => {
  const { data } = await instance.get(url, { params });
  return data;
};

export default instance;
